﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Auction
{
    public partial class FSearchPackage : Form
    {
        FormBase frmSuper;
        private EDM_AuctionContainer context;
        Package packageFound = null;
        Validations valid = new Validations();
        public FSearchPackage(FormBase frmSuper,EDM_AuctionContainer context)
        {
            InitializeComponent();
            this.frmSuper = frmSuper;
            this.context = context;
        }
        public FSearchPackage()
        {
            InitializeComponent();
        }
        private void b_SearchPackage_Click(object sender, EventArgs e)
        {
            lblAdvNotFound.Visible=false;
            Lnk_showMoreData.Visible = false;
            packageFound = null;
            if (rdbSearchById.Checked)
            {
                int id = valid.GetInt(t_id);
                try
                {
                    packageFound = context.PackageSet.FirstOrDefault(p => p.idPackage == id);
                }
                catch (System.Exception)
                {
                    packageFound=null;
                }
            }
            else if(rdbSearchByPackageName.Checked)
            {
                try
                {
                    packageFound = context.PackageSet.FirstOrDefault(p => p.nameOfPackage == t_Name.Text.ToString());
                }
                catch (System.Exception)
                {
                    packageFound = null;
                }
            }
           
            if (packageFound != null)
            {
                ((FormBase)this.frmSuper).TransferPackageFound(packageFound);
            }
            else
            {
                lblAdvNotFound.Visible = true;
            }
        }

        private void rdbSearchByPackageName_CheckedChanged(object sender, EventArgs e)
        {
             if (rdbSearchByPackageName.Checked)
            {
                grb_Name.Enabled = true;
                b_SearchPackage.Enabled = true;
            }
            else
            {
                grb_Name.Enabled = false;
            }
        }

        private void rdbSearchById_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbSearchById.Checked)
            {
                grb_ID.Enabled = true;
                b_SearchPackage.Enabled = true;
            }
            else
            {
                grb_ID.Enabled = false;
            }
        }
    }
}
